"""
model name : aihw
file       : utils.py
information:
    author : OuYang
    time   : 2025/10/10
"""


def index2corr(i, j, col=19, row=19):
    if i < 0 or j < 0 or i >= row or j >= col:
        raise IndexError('index out of range')
    x = j + 1
    y = row - i
    return x, y


def corr2index(x, y, col=19, row=19):
    if x <= 0 or y <= 0 or x > row or y > col:
        raise IndexError('corr out of range')
    i = col - y
    j = x - 1
    return i, j


def idx2rgba(i, mi, sc=(0, 255, 0), ec=(0, 0, 255)):
    assert 0 <= i <= mi

    if mi == 0:
        return sc

    r = i / mi

    R = int(sc[0] + (ec[0] - sc[0]) * r)
    G = int(sc[1] + (ec[1] - sc[1]) * r)
    B = int(sc[2] + (ec[2] - sc[2]) * r)
    A = max(int(255 * r), 100)

    R = max(0, min(255, R)) / 255.0
    G = max(0, min(255, G)) / 255.0
    B = max(0, min(255, B)) / 255.0
    A = max(0, min(255, A)) / 255.0

    return R, G, B, A


if __name__ == '__main__':
    print(index2corr(0, 18))  # 19, 19
    print(index2corr(18, 0))  # 1, 1
    print(corr2index(17, 19))  # 18, 0
    print(corr2index(19, 19))  # 0, 18
    print(corr2index(1, 1))
